package com.test.langchain4j.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 2025/8/11 14:50
 * @modifiedBy:
 * @version: 1.0
 */
@Configuration
public class CorsConfig {
  @Bean
  public CorsFilter corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    // 允许的源
    config.addAllowedOriginPattern("*");
    // 允许凭证（cookies等）
    config.setAllowCredentials(true);
    // 允许的HTTP方法
    config.addAllowedMethod("*");
    // 允许的请求头
    config.addAllowedHeader("*");
    // 预检请求的缓存时间
    config.setMaxAge(3600L);

    source.registerCorsConfiguration("/ollama/**", config);
    return new CorsFilter(source);
  }
}
